/**
 * ImportData.java created by zhangzhidong 
 * at 下午1:09:14 2016年7月10日
 */
package test;

import java.io.File;
import java.io.FilenameFilter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import com.mysql.jdbc.Statement;

/**
 * @author zhangzhidong<br>
 * comment generated at 2016年7月10日下午1:09:14<br>
 * 
 */
public class ImportData {

	
	
	public static String dir = "/Users/zhangzhidong/Desktop/Pics";
	/**
	 * 下午1:09:15 2016年7月10日
	 * @param args
	 */
	static void print(Object o){
		System.out.println(o);
	}
	static String sqlformat = "insert into tinytimes.home(path,date,comment,filename,province,city,district) values(%s,%s,%s,%s,%s,%s,%s);";
	public static void main(String[] args) throws ParseException, ClassNotFoundException, SQLException {
		
		
		
		//importData(new File(dir));
	}
	static void importData(File dir) throws ParseException, ClassNotFoundException, SQLException{
			File fs [] = dir.listFiles(new FilenameFilter(){
				public boolean accept(File dir, String name) {
					// TODO Auto-generated method stub
					if(name.startsWith(".")) return false;else return true;
				};
			});
			String pics = fs[0].getParentFile().getName();
			List<String> allsqls = new ArrayList<String>(1000);
			for (File f :fs){
				String sub = f.getName();//location,date
				
				executeBatch(allsqls);
			}
			
			//
	}
	static void executeBatch(List<String> sqls) throws SQLException, ClassNotFoundException{
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection  
		        ("jdbc:mysql://103.240.17.161/tinytimes","website","zzd5211314"); 
		 Statement stmt = (Statement) conn.createStatement(); 
		 for(String sql:sqls){
			 stmt.addBatch(sql);
		 }
		 stmt.executeBatch();
		 stmt.close();

	}
	static String datefor = "yyyy-MM-dd H:m:s";
	static SimpleDateFormat sdf = new SimpleDateFormat(datefor,Locale.CHINA);
	static String [] listAllPicFiles(File f,String path,String pro,String city,String dis,String date){
		date = date.replace("年", "-");
		date = date.replace("月", "-");
		date = date.replace("日", "");
		date += " 00:00:00";
		File pics [] = f.listFiles(new FilenameFilter(){
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				if(name.toLowerCase().endsWith(".jpg")){
					return true;
				}
				return false;
			}});
		String sqls[] = new String[pics.length];
		int index=0;
		for(File pic:pics){
			sqls[index++] = String.format(sqlformat,"\"" + path + "\"", "\""+ date+"\"","\"no comment\"","\""+pic.getName()+"\"",pro==null?"\"\"":"\""+pro+"\"",city==null?"\"\"":"\""+city+"\"",dis==null?"\"\"":"\""+dis+"\"");
		}
		return sqls;
	}
	//(path,date,comment,filename,province,city,district) values(%s,%s,%s,%s,%s,%s,%s)";
/*
 * 
 * if(sub.contains(",")){
					String parts[]  = sub.split(",");
					String location  = parts[0];
					String pcd[] = location.split(" - ");
					String date = parts[1].trim();
					date = date.replace("年", "-");
					date = date.replace("月", "-");
					date = date.replace("日", "");
					
					String newname = date;
					
					if(!f.renameTo(new File(f.getParentFile(),date))){
						print("failed");
					};
					//allsqls.addAll(Arrays.asList(listAllPicFiles(f, new File(pics,newname).getName(), pcd[0], pcd[1], pcd[2], date)));
				}else{
					sub = sub.replace("年", "-");
					sub = sub.replace("月", "-");
					sub = sub.replace("日", "");
					if(!f.renameTo(new File(f.getParentFile(),sub))){
						print("failed");
					};
					//allsqls.addAll(Arrays.asList(listAllPicFiles(f,new File(pics,sub).getName(),null,null,null,sub)));
				}
 */
}
